package com.gxa.service.impl;

import com.gxa.dao.CommunityDao;
import com.gxa.dao.impl.CommunityDaoImpl;
import com.gxa.dto.CommunityDto;
import com.gxa.entity.Community;
import com.gxa.entity.extend.QueryAllCommunity;
import com.gxa.exception.ErrorType;
import com.gxa.exception.ServiceException;
import com.gxa.service.CommunityService;
import com.gxa.vo.CommunityVo;

import java.sql.SQLException;
import java.util.List;

public class CommunityServiceImpl implements CommunityService {
    private CommunityDao communityDao = new CommunityDaoImpl();

    @Override
    public List<QueryAllCommunity> queryCommunity() throws SQLException {
        CommunityDao communityDao = new CommunityDaoImpl();
        List<QueryAllCommunity> commList = communityDao.queryCommunity();
        return commList;
    }

    @Override
    public List<Community> queryAllCommunity() throws SQLException {
        return communityDao.selectAlLCommunity();
    }

    @Override
    public List<QueryAllCommunity> queryCommunity(Integer page, Integer limit) throws SQLException {
        CommunityDao communityDao = new CommunityDaoImpl();
        List<QueryAllCommunity> commList = communityDao.queryCommunity(page, limit);
        return commList;
    }

    @Override
    public List<QueryAllCommunity> queryCommunity(Integer page, Integer limit, CommunityDto communityDto) throws SQLException {
        CommunityDao communityDao = new CommunityDaoImpl();
        List<QueryAllCommunity> commList = communityDao.queryCommunity(page, limit, communityDto);
        return commList;
    }

    @Override
    public List<CommunityVo> queryAllCommunityByPageAndComDto(String page, String limit, CommunityDto communityDto) {
        // 1、非空校验
        if (page == null || page.trim().isEmpty()) {
            throw new ServiceException(ErrorType.EMPTY_QUERY_PAGE);
        }
        if (limit == null || limit.trim().isEmpty()) {
            throw new ServiceException(ErrorType.EMPTY_QUERY_PAGE);
        }

        // 2、参数转换
        int pageToInt = Integer.parseInt(page);
        int limitToInt = Integer.parseInt(limit);

        // 3、调用Dao完成业务
        List<CommunityVo> communities = communityDao.selectAlLCommunityByPageAndDto(pageToInt,limitToInt,communityDto);


        return communities;
    }

    @Override
    public Integer count() throws SQLException {
        CommunityDao communityDao = new CommunityDaoImpl();
        Integer count = communityDao.count();
        return count;
    }

    @Override
    public Integer count(CommunityDto communityDto) throws SQLException {
        CommunityDao communityDao = new CommunityDaoImpl();
        Integer count = communityDao.count(communityDto);
        return count;
    }

    @Override
    public void add(Community community) throws SQLException {
        CommunityDao communityDao = new CommunityDaoImpl();
        communityDao.add(community);
    }

    @Override
    public Community queryByID(Integer cid) throws SQLException {
        CommunityDao communityDao = new CommunityDaoImpl();
        Community community = communityDao.queryByID(cid);
        return community;
    }

    @Override
    public void update(Community community) throws SQLException {
        CommunityDao communityDao = new CommunityDaoImpl();
        communityDao.update(community);
    }

    @Override
    public void delete(Integer cid) throws SQLException {
        CommunityDao communityDao = new CommunityDaoImpl();
        communityDao.delete(cid);
    }

}
